package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.module.dict.entity.SysDictData;
import cn.xenosp.server.module.dict.entity.SysDictDataVO;
import cn.xenosp.server.module.dict.service.SysDictDataService;
import cn.xenosp.server.module.login.service.LoginService;
import cn.xenosp.server.module.user.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-12-04 10:00
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/common/")
public class CommonController {

    @Autowired
    SysDictDataService sysDictDataService;
    @Autowired
    LoginService loginService;

    @PostMapping("/getDataByDictType")
    public Result<?> getDictDataByType(@RequestBody SysDictDataVO sysDictDataVO){
        try {
            List<Map<String, Object>> list =  sysDictDataService.listMaps(new LambdaQueryWrapper<SysDictData>()
                    .select(SysDictData::getLabel,SysDictData::getValue)
                    .eq(SysDictData::getDictType, sysDictDataVO.getDictType()));
            return Result.OK("",list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取字典数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "公共模块",operationName = "刷新缓存",operationContent = "刷新缓存数据")
    @PostMapping("/refresh")
    public Result<?> refresh(){
        try {
            loginService.refresh();
            return Result.OK("",null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("刷新失败",null);
        }
    }
}
